/*
 * Class ItemSelect
 *
 * Version 1.0
 *
 * Friday, August 22, 2008
 *
 * Created by Palidino76
 */

package palidino76.rs2.io.packets;

import palidino76.rs2.Engine;
import palidino76.rs2.players.Player;
import palidino76.rs2.players.items.PlayerItems;
import palidino76.rs2.util.Misc;

public class ItemSelect implements Packet {
    public void handlePacket(Player p, int packetId, int packetSize) {
        if (p == null || p.stream == null) {
            return;
        }
	PlayerItems pi = new PlayerItems();
        int junk = p.stream.readUnsignedByte();
        int interfaceId = p.stream.readUnsignedWord();
        junk = p.stream.readUnsignedByte();
        int itemId = p.stream.readUnsignedWordBigEndian();
        int itemSlot = p.stream.readUnsignedWordA();
	p.attackPlayer = 0;
        p.attackingPlayer = false;
        if (itemSlot < 0 || itemSlot > p.items.length || p.items[itemSlot] != itemId) {
            return;
        }
        if (p.isDead || p.skillLvl[3] < 1) {
            return;
        }

        if (interfaceId == 149) {
            switch (itemId) {
				case 391:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(22, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 385:
                case 397:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(20, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 315:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(3, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 319:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(1, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 325:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(4, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 329:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(9, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 339:
                case 333:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(7, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 347:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(5, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 351:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(8, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 355:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(6, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 361:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(10, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 365:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(13, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 7946:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(16, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 379:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(12, true);
                        p.requestAnim(829, 0);
                    }
                break;
                case 373:
                    if(p.eatDelay <= 0)
                    {
                        p.eatDelay = 3;
                        p.combatDelay += 2;
                        pi.deleteItem(p, itemId, itemSlot, 1);
                        p.updateHP(16, true);
                        p.requestAnim(829, 0);
                    }
                break;
            default:
                Misc.println("[" + p.username + "] Unhandled item selected: " + interfaceId + ":" + itemId + ":" + itemSlot);
                break;
            }
        } else {
            Misc.println("[" + p.username + "] Unhandled item select " + interfaceId + ":" + itemId);
        }

        pi = null;
    }
	
	public void changeStat(Player p, int stat, int amt, int type, boolean bol)
    {
        if(p == null)
        {
            return;
        }
        if(bol)
        {
            if(p.skillLvl[stat] >= (p.getLevelForXP(stat) + amt))
            {
                return;
            }
            p.skillLvl[stat] += amt;
            if(p.skillLvl[stat] >= (p.getLevelForXP(stat) + amt))
            {
                p.skillLvl[stat] = (p.getLevelForXP(stat) + amt);
            }
            if(type == 0)
            {
                if(p.skillLvl[stat] > p.getLevelForXP(stat))
                {
                    p.skillLvl[stat] = p.getLevelForXP(stat);
                }
            }
        }
        else if(!bol)
        {
            p.skillLvl[stat] -= amt;
            if(p.skillLvl[stat] < 1)
            {
                p.skillLvl[stat] = 1;
            }
        }
        p.frames.setSkillLvl(p, stat);
    }
}
